package dell.yangzhou01;

import java.io.StringReader;
import java.util.Optional;

public class OptionalTest {
    public static void main(String[] args) {
        Girl girl=new Girl();
        Optional<Object> optionalGirl = Optional.of(girl);
        //参数不可以为null，为null时会抛出异常
        System.out.println(optionalGirl);
        Optional<Object> optionalGirl1 = Optional.ofNullable(null);
        //参数可以为null，输出:Optional.empty
        System.out.println(optionalGirl1);
        System.out.println("====================");
        Girl girl1=new Girl();
        girl1=null;
        Optional<Girl> girlo1 = Optional.ofNullable(girl1);
        Girl girlo2 = girlo1.orElse(new Girl("养的备胎！"));
        //orElse（T t1）如果当前的Optional内部封装的t是非空的，就返回内部的t，
        //如果内部封装的t是空的，就返回orElse（）方法中的t1
        System.out.println(girlo2);
        //【注意只输出orElse（）方法得到的哪一个参数就可以了，很简洁方便】
    //使用Optional类的getGirlName（）；
        Boy boy=null;
        System.out.println("===========");

        String str="hello";
        //str此时不能为空，否则就会抛出异常
        Optional<String> str1 = Optional.of(str);
        String s = str1.get();
        System.out.println(s);

        String str2="字符串";
        //str2=null;//打印输出：字符串的备胎
        Optional<String> stri2 = Optional.ofNullable(str2);
        String orElse = stri2.orElse("字符串的备胎");
        System.out.println(orElse);


    }
    public String getGirlName(Boy boy){
        Optional<Boy> boy1 = Optional.ofNullable(boy);
        Boy orElse1 = boy1.orElse(new Boy(new Girl("备胎中的备胎")));
        Girl girl=new Girl();
        Optional<Girl> optionalGirl = Optional.ofNullable(girl);
        Girl orElse = optionalGirl.orElse(new Girl("最里面的备胎"));
            return orElse.getName();
    }
}
